$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        id:null,
        modelTr:null,
        init:function () {
            this.form = new xhForm($('#xh_form')).parse();
            this.modelTr = $('.modelTr').eq(0).clone();
            $('.prodName').eq(0).select2();
            this.id = xhTool.getQuery('id');
            this.listener();
            if (formData) {
                this.funObj().initEditForm(formData);
            }
        },
        listener:function () {
            var $this = this;
            //double处理
            $('.doubleInput').on('change',function () {
                $this.funObj().toFormatDouble($(this).val(), $(this));
            });

            $('#bt_modal_confirm', parent.document).unbind('click').bind('click',function () {
                $this.funObj().submitFormAction();
            });

            $('#xh_form').on('change','.prodName',function () {
                $this.funObj().setResNum($(this));
            });

            $('#addBtn').click(function () {
                $this.funObj().addProd();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                toFormatDouble:function (number,dom) {
                    var f = parseFloat(number);
                    if (isNaN(f)) {
                        dom.val('0.00');
                    }
                    var f = Math.round(number*100)/100;
                    var s = f.toString();
                    var rs = s.indexOf('.');
                    if (rs < 0) {
                        rs = s.length;
                        s += '.';
                    }
                    while (s.length <= rs + 2) {
                        s += '0';
                    }
                    dom.val(s);
                },
                addProd:function (row) {
                    if (!row) {
                        var dom = $($this.modelTr.clone());
                        $('#addBtn').parent().parent().before(dom);
                        dom.find('.prodName').select2();
                    } else {

                    }

                },
                setResNum:function (target) {
                    var dom = $(target);
                    var val = dom.val();
                    var numDom = dom.parent().parent().find('.resNum');
                    if(val !==''){
                        xhExtra.ajaxRequest('ims/Produce/getProdNum', {id:val}, {}, function (res) {
                            numDom.text(res.num);
                        })
                    }
                },
                getCusProdData:function () {
                    var $arr = [];
                    $('.modelTr').each(function () {
                        var obj = {};
                        var dom = $(this);
                        obj.prodId = dom.find('.prodName').val();
                        obj.num    = dom.find('.num').val();
                        if (obj.num > 0 && obj.prodId) {
                            $arr.push(obj);
                        }
                    });
                    return $arr;
                },
                initEditForm:function (formData) {
                    $this.form.fill(formData);
                    if(formData.mParts.length > 0) {
                        $.each(formData.mParts, function (i,item) {
                            $this.funObj().addProd();
                            var dom = $('.modelTr').eq(i);
                            dom.find('.prodName').val(item['prodId']).trigger('change');
                            dom.find('.num').val(item['num']);
                        });
                        $('.modelTr:last').remove();
                    }
                },
                submitFormAction:function () {
                    var $params = {};
                    if ($this.id) {
                        $params.iProdId = $this.id;
                    }
                    $params.prods = $this.funObj().getCusProdData();
                    $this.form.submit('ims/produce/saveProduce', $params, function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.prodDataGrade.reload();
                        window.top.xhTop.closeModal();
                    })
                }
            }
        }
    }
};
